var Connection=function(config){
	Ext.apply(this,config)
	var msg='正在发送并处理数据... ...';
	if('string'==typeof(this.waitMsg)){
		msg=this.waitMsg;
	}
	this.submit=function(){
		if(this.model!=false)
		Ext.Msg.wait(msg, '请稍等');
		Ext.Ajax.request({
			clientValidation: true,
			url: this.url,
			method: 'POST',
			params: this.params,
			callback: this.callback
		});
	}
}

var msg = function(title, msg) {
  Ext.Msg.show({
        title : title,
        msg : msg,
        minWidth : 200,
        modal : true,
        icon : Ext.Msg.INFO,
        buttons : Ext.Msg.OK
      });
};

function convertToArray(obj){
	 if (!Ext.isArray(obj)) {
        obj = [obj];
    }
	return obj;
}

/**将Argument对象软的为数组 */
function argumentsToArray(arg){
	var obj=[];
	if(arg.length>0){
		for(var i=0,len=arg.length;i<len;i++){
			obj[i]= arg[i];
		}
	}
	return obj;
}
/**方法链，可在未执行前动态添加命令； */
function CommandLink(){
	this.commondArray=[];
	/** 添加执行命令；*/
	this.addCommond=function(cmd){
		this.commondArray[this.commondArray.length]=cmd;
	}
	/** 清除所有的执行命令；*/
	this.clear=function(){
		this.commond=[];
	}
	
	/** 实际的执行；会按传入的参数来匹对执行方法应该传入的方法*/
	this.doCommand=function(obj){
		var i=0;
		while(this.commondArray[i]!=null){
			var rs=true;
			try {
				if(typeof(obj)!='object')
					rs=this.commondArray[i++]();
				else if(obj.length==1)
					rs=this.commondArray[i++](obj[0]);
				else if(obj.length==2)
					rs=this.commondArray[i++](obj[0],obj[1]);
				else if(obj.length==3)
					rs=this.commondArray[i++](obj[0],obj[1],obj[2]);
				else if(obj.length==4)
					rs=this.commondArray[i++](obj[0],obj[1],obj[2],obj[3]);
				else if(obj.length==5)
					rs=this.commondArray[i++](obj[0],obj[1],obj[2],obj[3],obj[4]);
				else if(obj.length==6)
					rs=this.commondArray[i++](obj[0],obj[1],obj[2],obj[3],obj[4],obj[5]);
			}catch(e){log.error("注入方法执行出错："+e)}
			if (false == rs) {
				return;
			}
			
		}
	}
	var that=this;
	this.action=function(){
		that.doCommand(arguments);
	}
}

/**按钮操作工具类(工厂类)  */
function buttonFactory(){
	this.buttonMap=new Ext.util.MixedCollection();//按钮对象池;ID为KEY,对象为值
	this.aciontMap=new Ext.util.MixedCollection();//事件对象池;ID+事件保为KEY
	/**创建工厂,在对象中给记录.更容易后面的操作 */
	this.createButton=function(config){
		var button=new Ext.Button(config);
		this.buttonMap.add(config.id,button);
		return button;
	}
	/**开启按钮对象 */
	this.enableButton=function (){
		var that=this;
		try {
			ids = argumentsToArray(arguments);
			Ext.each(ids, function(id){
				var bt=that.getButton(id).enable();
			})
		}catch(e){log.error("button enable:"+e)}
	}
	/**关闭按钮对象 */
	this.disableButton=function () {
		var that=this;
		try {
			ids = argumentsToArray(arguments);
			Ext.each(ids, function(id){
				var bt = that.getButton(id).disable();
			})
		} 
		catch (e) {
		}
	}
	/**得到按钮对象 */
    this.getButton=function (id){
        var bt = null;
        try {
            bt = this.buttonMap.get(id)
        }  catch (e) {
            try {
                bt = Ext.getCmp(id);
				this.buttonMap.add(id,bt);
            } catch (e) { log.error("button 获取异常:" + e) }
        }
		return bt;
    }
	/**按钮对象添加事件响应 */
	this.addListener=function (id,action,fun){
		this.addActions(this.getButton(id),action,fun);
	}
	/**所有的托管按钮对象添加事件响应 */
	this.addAllListener=function (action,fun){
		var at=action,f=fun;
		var that=this;
		this.buttonMap.each(function(bt , index , length ){
			that.addActions(bt,at,f);
		})
	}
	/**私有方法为托管按钮对象添加事件响应 */
	this.addActions=function(bt,action,fun){
		var action=this.getAction(bt,action);
		action.addCommond(fun);
	}
	
	this.getAction=function(bt,action){
		var key=bt.id+","+action;
		var command=null;
		try {
			command=this.aciontMap.get(key)
			if(typeof(command)!='object'){
				command=new CommandLink();
				this.aciontMap.add(key,command)
				bt.on(action,command.action)
			}
		}catch(e){log.error("add action:"+e)};
		return command;
	}
}


/**去除返回JSON上firefox自动增加的<prex>符号; */
Ext.override(Ext.form.Action.Submit, {
    // private  
    processResponse: function(response){
        this.response = response;
        //增加下面几句代码就OK啦  
        ////////////////////////  
        var data = response.responseText;
        if (data.indexOf('<pre>') != -1) {
            response.responseText = data.substring(5, data.length - 6);
            this.response = Ext.util.JSON.decode(response.responseText);
        }
        ///////////////////////////       
        if (!response.responseText) {
            return true;
        }
        this.result = this.handleResponse(response);
        return this.result;
    }
});
